
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.server.RMIClassLoader;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.io.DataInputStream;
import java.io.IOException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ntthinh
 */
public class DictClient {

    public static void main(String args[]) throws Exception {
        //declarer les variables
        DataInputStream in = new DataInputStream(System.in);
        Dictionnary dict = null;
        //
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }
        try {
            
            Registry registry = LocateRegistry.getRegistry(args[0]);
			String name = args[1];//"DictService";
            dict = (Dictionnary) registry.lookup(name);
			
            //dict=(Dictionnary) Naming.lookup("rmi://localhost:1099/DictService");
            /*
            } catch (MalformedURLException murle) {
            System.out.println();
            System.out.println(
            "MalformedURLException");
            System.out.println(murle);
             */
        } catch (RemoteException re) {
            System.out.println();
            System.out.println(
                    "RemoteException");
            System.out.println(re);
        } catch (NotBoundException nbe) {
            System.out.println();
            System.out.println(
                    "NotBoundException");
            System.out.println(nbe);
        }
        //affichier le menu

        showMainMenu();
        while (true) {
            System.out.print("Command:");
            String cmd = in.readLine();
            // int cmd= Integer.parseInt(strCmd);
            /*

            System.out.println("[2] Help");
            System.out.println("[3] Exit");
            System.out.println("[4] Add new entity");


            System.out.println("[7] Sort dictionnary");

            System.out.println("[9] Save dictionnary");
            System.out.println("[10] First entity");
            System.out.println("[11] Last entity");
            System.out.println("[12] next entity");
            System.out.println("[13] Previous entity");
            System.out.println("[14] Total entity");
             */
            try {
                //rechercher une untite
                if (cmd.equalsIgnoreCase("1")) {
                    System.out.println("-----------------------");
                    System.out.println("[1] Find an entity");
                    System.out.print("Enter an entity:");
                    String word = in.readLine();
                    Entity entity = dict.findEntity(word);
                    displayEntity(entity);
                    //help
                    } else if (cmd.equalsIgnoreCase("2")) {
                    showMenu();
                } else if (cmd.equalsIgnoreCase("3")) {
                    System.exit(0);
                    //ajouter une entite
                } else if (cmd.equalsIgnoreCase("4")) {
                    // DictClient.addEntity(in, dict);
                    //entrer le term
                    String term = Util.readTerm(in);
                    //entrer la categorie
                    CategoryType categ = Util.readCategoryType(in);
                    PropertyImpl[] props = new PropertyImpl[2];
                    props[0] = new PropertyImpl();
                    props[1] = new PropertyImpl();
                    //entrer les priprietes correspondantes
                    switch (categ) {
                        case NOM:

                            props[0].setPropertyType(PropertyType.GENRE);
                            props[0].setGenre(Util.readGenre(in));
                            //props[1] = new PropertyImpl();
                            props[1].setPropertyType(PropertyType.PLURIEL);
                            props[1].setPluriel(Util.readPluriel(in));
                            break;
                        case ADJ:
                            //props[0] = new PropertyImpl();
                            props[0].setPropertyType(PropertyType.FEMININ);
                            props[0].setFeminin(Util.readFeminin(in));
                            //props[1] = new PropertyImpl();
                            props[1].setPropertyType(PropertyType.PLURIEL);
                            props[1].setPluriel(Util.readPluriel(in));
                            break;
                        case VERB:
                            //props[0] = new PropertyImpl();
                            props[0].setPropertyType(PropertyType.GROUPE);
                            props[0].setGroupe(Util.readGroupe(in));
                            //props[1] = new PropertyImpl();
                            props[1].setPropertyType(PropertyType.TRANSIVITE);
                            props[1].setTrans(Util.readTransitive(in));
                            break;
                    }
                    //entrer la definition
                    String def = Util.readDefinition(in);
                    //ajouter cet entite
                    dict.addEntity(term, categ, props, def);

                } //2-supprimer une entite
                else if (cmd.equalsIgnoreCase("5")) {
                    dict.delEntity(Util.readTerm(in));
                    System.out.println("-----------------------");
                    System.out.println("[5] Delete an entity");
                    System.out.println("Delete successfully!");
                }//3-modifier une entite
                else if (cmd.equalsIgnoreCase("6")) {
                    System.out.println("-----------------------");
                    System.out.println("[6] Modify the definition of entity");
                    System.out.print("Enter an entity modified:");
                    String old = in.readLine();
                    System.out.print("New definition:");
                    String def = in.readLine();
                    dict.modifDefinition(old, def);
                    System.out.println("Modify successfully!");

                    //4-tirer ce dictionnaire
                } else if (cmd.equalsIgnoreCase("7")) {
                    //long n = dict.sortDict(true);
                    //System.out.println("Dict loaded, total " + n + "  entries!");
                    //charger
                } else if (cmd.equalsIgnoreCase("8")) {
                    System.out.println("-----------------------");
                    System.out.println("[8] Load dictionnary");
                    long n = dict.loadDict();
                    System.out.println("Load successfully");
                    //sauvegarder
                } else if (cmd.equalsIgnoreCase("9")) {
                    System.out.println("-----------------------");
                    System.out.println("[9] Save dictionnary");
                    dict.saveDict();
                    System.out.println("Save successfully!");

                } else if (cmd.equalsIgnoreCase("10")) {
                    System.out.println("-----------------------");
                    System.out.println("[10] First entity");
                    Entity entity = dict.fistEntity();
                    System.out.print("The first entity:");
                    displayEntity(entity);
                    //retourner la derniere entite
                } else if (cmd.equalsIgnoreCase("11")) {
                    System.out.println("-----------------------");
                    System.out.println("[11] Last entity");
                    Entity entity = dict.lastEntity();
                    System.out.print("The last entity");
                    displayEntity(entity);
                    //retourner next entite
                } else if (cmd.equalsIgnoreCase("12")) {
                    System.out.println("-----------------------");
                    System.out.println("[12] next entity");
                    Entity entity = dict.nextEntity();
                    System.out.print("The next entity");
                    displayEntity(entity);
                    //retourner prev entite
                } else if (cmd.equalsIgnoreCase("13")) {
                    System.out.println("-----------------------");
                    System.out.println("[13] Previous entity");

                    Entity entity = dict.prevEntity();
                    displayEntity(entity);
                    //afficher le nombre des entites
                } else if (cmd.equalsIgnoreCase("14")) {
                    System.out.println("-----------------------");
                    System.out.println("[14] Total entity");
                    long n = dict.numEntities();
                    System.out.println("Total: " + n + " entries ");
                    // exister

                } else {
                    System.out.println("Invalid command, type 2 to help");
                }

                //Exception
            } catch (NumberFormatException ndeEx) {
                System.out.println("Invalid command, please try again!");

                //il n'existe pas ce term
            } catch (TermNotFound tnfE) {
                System.out.println("Error: TermNotFound");
            } catch (DictEmpty deEx) {
                System.out.println("Error: Dictionary is empty");
            } catch (DictFilesNotFound dfEx) {
                System.out.println("Error: Dictionary's file not found");
            } catch (TermExisted teEx) {
                System.out.println("Error: Term is existed");
            } catch (Exception e) {
                System.out.println("Error:" + e);
                e.printStackTrace();
            }

        }


    }
    /*
     * displayEntity : affichier des informations d'une entite
     */

    public static void displayEntity(Entity entity) throws Exception {
        String term = entity.myEntry().getTerm();
        System.out.println("------------------------------");
        System.out.println("Entity's Information");
        System.out.println("Term: " + term);
        CategoryType categ = entity.myEntry().myCategory().getCategory();
        Property[] props = entity.myEntry().myProperties();
        switch (categ) {
            case NOM:
                System.out.println("Category: Noun");
                System.out.println("Genre: " + props[0].printString());
                System.out.println("Plural: " + props[1].printString());
                break;
            case ADJ:
                System.out.println("Category: Adjective");
                System.out.println("Feminine: " + props[0].printString());
                System.out.println("Plural: " + props[1].printString());
            case VERB:
                System.out.println("Category: Verb");
                System.out.println("Group: " + props[0].printString());
                System.out.println("Transitivite: " + props[1].printString());
        }
        System.out.println("Definition:" + entity.myDefinition());
        System.out.println("------------------------------");

    }

    public static void showMenu() {
        System.out.println("Welcome to Dictionnary Service");
        System.out.println("******************************");
        System.out.println("Please select one command");
        System.out.println("[1] Find an entity");
        System.out.println("[2] Help");
        System.out.println("[3] Exit");
        System.out.println("[4] Add new entity");
        System.out.println("[5] Delete an entity");
        System.out.println("[6] Modify the definition of entity");
        System.out.println("[7] Sort dictionnary");
        System.out.println("[8] Load dictionnary");
        System.out.println("[9] Save dictionnary");
        System.out.println("[10] First entity");
        System.out.println("[11] Last entity");
        System.out.println("[12] next entity");
        System.out.println("[13] Previous entity");
        System.out.println("[14] Total entity");

    }

    public static void showMainMenu() {
        System.out.println("Welcome to Dictionnary Service");
        System.out.println("******************************");
        System.out.println("Please select one command");
        System.out.println("[1] Find an entity");
        System.out.println("[2] Help");
        System.out.println("[3] Exit");
    }
}
